package com.totoro.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import java.util.Properties;

/**
 *
 * Created by Niki on 2018/1/19 9:27
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.totoro.project.controller"})
public class SimpleMappingExceptionResolverConf {

    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){
        SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
        Properties mappings = new Properties();

        simpleMappingExceptionResolver.setDefaultErrorView("page_505");
        simpleMappingExceptionResolver.setDefaultStatusCode(505);

        mappings.put("org.springframework.web.servlet.PageNotFound", "page-404"); //页面没找到异常，统一返回page-404异常页面
        mappings.put("org.springframework.dao.DataAccessException","data-access"); //页面请求参数异常，统一返回data-access异常页面
        //... 对应各种不同的异常，定义不同的处理页面

        simpleMappingExceptionResolver.setExceptionMappings(mappings);
        return simpleMappingExceptionResolver;
    }
}

